package com.fenyin.samples.study.json;  

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fenyin.samples.study.util.TimeUtil;
  
public class Example {
	
	private static final Log log = LogFactory.getLog(Example.class);
	
	private static String convertMsg(){
		Map<String,Object> map=null;
		List<Map<String,Object>> params=new ArrayList<Map<String,Object>>();
		map=new LinkedHashMap<String, Object>();
		map.put("infoId", 12345678L);
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("userId", 2222222L);
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("userIp", "192.168.119.190");
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("opReason", "正常的删除，不要纠结。");
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("systemSource", 1);
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("opType", 1);
		params.add(map);
		
		map=new LinkedHashMap<String, Object>();
		map.put("opTime", TimeUtil.getCurrentTime17ByMillis());
		params.add(map);
		
		JSONArray jsonObject=new JSONArray(params);
		return jsonObject.toString();
	}
	
	private static String convertObjMsg() throws JSONException{
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("infoId", 12345678L);
		jsonObject.put("userId", 2222222L);
		jsonObject.put("userIp", "192.168.119.190");
		jsonObject.put("opReason", "{opUserId:\"111111\",opDesc:\"管理员后台信息管理删除\"}");
		jsonObject.put("systemSource", 1);
		jsonObject.put("opType", 1);
		jsonObject.put("opTime", TimeUtil.getCurrentTime17ByMillis());
		return jsonObject.toString();
	}
	
	private static String parseMessage(String message){
		try {
			JSONObject jsonObject = new JSONObject(message);
			long infoId = jsonObject.getLong("infoId");
			long userId = jsonObject.getLong("userId");
			String userIp=jsonObject.getString("userIp");
			String opReason=jsonObject.getString("opReason");
			int systemSource=jsonObject.getInt("systemSource");
			short opType = (short) jsonObject.getInt("opType");
			String opTime = jsonObject.getString("opTime");
			StringBuffer sb = new StringBuffer();
			sb.append("infoId").append(":").append(infoId).append(",");
			sb.append("userId").append(":").append(userId).append(",");
			sb.append("userIp").append(":").append(userIp).append(",");
			sb.append("opReason").append(":").append(opReason).append(",");
			sb.append("systemSource").append(":").append(systemSource).append(",");
			sb.append("opType").append(":").append(opType).append(",");
			sb.append("opTime").append(":").append(opTime).append(",");
			return sb.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	private static String parseReason(String message){
		try {
//			{opUserId:"管理员ID",opUserName:"管理员名称",opDesc:"操作原因"}
			JSONObject jsonObject = new JSONObject(message);
			String opUserId=jsonObject.getString("opUserId");
			String opUserName=jsonObject.getString("opUserName");
			String opDesc=jsonObject.getString("opDesc");
			StringBuffer sb = new StringBuffer();
			sb.append("opUserId").append(":").append(opUserId).append(",");
			sb.append("opUserName").append(":").append(opUserName).append(",");
			sb.append("opDesc").append(":").append(opDesc).append(",");
			return sb.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public static void main(String[] args) throws JSONException{
		log.info("result:"+convertObjMsg());
		log.info(parseMessage(convertObjMsg()));
		log.info(parseReason("{opUserId:\"管理员ID\",opUserName:\"管理员名称\",opDesc:\"操作原因\"}"));
		
	}
}
